<?php

/**
 * facebook actions.
 *
 * @package    preuvideo
 * @subpackage facebook
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class facebookActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->forward('default', 'module');
    }

    public function executeLogin() {
        $fb_user = $this->check_connection();

        if (isset($fb_user)) {
            $access_token = $_SESSION['access_token'];
            $my_url = urlencode("http://preuvideo.localhost/frontend_dev.php");
        } else {
            $fb_user = null;
            $access_token = null;
            $my_url = null;
        }
        $parametros = array('fb_user' => $fb_user,
            'access_token' => $access_token,
            'my_url' => $my_url
        );
        return $this->renderPartial('login', $parametros);
    }

    public function executeFbLogin(sfWebRequest $request) {

        $app_id = sfConfig::get('app_facebook_id');
        $app_secret = sfConfig::get('app_facebook_secret');
        $my_url = "http://preuvideo.localhost/frontend_dev.php/facebook/fbLogin";

        session_name($_SERVER["REMOTE_ADDR"] . "-" . $_SERVER["preuvideo"]);
        session_start();

        $code = $_REQUEST["code"];


        if (empty($code)) {
            $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
            $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
                    . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
                    . $_SESSION['state'];

            $serialized_url = serialize($request->getParameter('url'));
            $_SESSION['url'] = $serialized_url;

            //echo '<div>'.$dialog_url.'</div>';
            //http://www.facebook.com/dialog/oauth?client_id=209824272419810&redirect_uri=http%3A%2F%2Fpreuvideo.localhost%2Ffrontend_dev.php%2Fhome%2FfbLogin&state=a05783c576ac2f6ccba1e96eb92d6327
            $this->action_login = 1;
            //exit;
            $this->redirect($dialog_url);
            //echo("<script> top.location.href='" . $dialog_url . "'</script>");
        } elseif ($_REQUEST['state'] == $_SESSION['state']) {
            $token_url = "https://graph.facebook.com/oauth/access_token?"
                    . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
                    . "&client_secret=" . $app_secret . "&code=" . $code;

            $response = file_get_contents($token_url);
            $params = null;

            echo '<div>' . $response . '</div>';

            parse_str($response, $params);



            $_SESSION['access_token'] = $params['access_token'];

            $graph_url = "https://graph.facebook.com/me?access_token=" . $_SESSION['access_token'];
            $user = json_decode(file_get_contents($graph_url));
            if ($user) {
                $_SESSION["fb_name"] = $user->name;
                $_SESSION["fb_id"] = $user->id;
            }


            //redirigir adonde se venia.
            $url = unserialize($_SESSION['url']);

            if($url == 'http://preuvideo.asamblea.cl/'){
                $this->redirect('home/index');
            }


            $pedacitos = explode('/',$url);

            $n = 3;

            //error_log($pedacitos[$n]);
            if($pedacitos[$n] == 'frontend_dev.php'){
                $n += 1;
            }
            
            $parametros;
            
            if(array_key_exists($n,$pedacitos)){
                $parametros['module'] = $pedacitos[$n];
            }
            else{
                $this->redirect('home/index');

            }

            if(array_key_exists($n+1,$pedacitos)){
                $parametros['action'] = $pedacitos[$n+1];
            }

            foreach($pedacitos as $i => $pedacito){
                if($i > $n+1){
                    if($i%2 != 0){
                        $parametros[$pedacito] = $pedacitos[$i+1];
                    }
                }
            }

            foreach($parametros as $i => $parametro){
                error_log($i.'. '.$parametro);
            }

            $this->redirect($this->generateUrl('default', $parametros));
            //$user = json_decode(file_get_contents($graph_url));

        } else {
            echo("The state does not match. You may be a victim of CSRF.");
        }
    }

    public function check_connection() {

        //if(session_id()==""){
        session_name($_SERVER["REMOTE_ADDR"] . "-preuvideo");
        session_start();
        //}
        if (isset($_SESSION['state']) && isset($_SESSION['access_token'])) {
            if ($_SESSION['state'] && $_SESSION['access_token']) {
                $graph_url = "https://graph.facebook.com/me?access_token="
                        . $_SESSION['access_token'];
                $user = json_decode(file_get_contents($graph_url));
                if ($user) {
                    return $user;
                } else {
                    unset($_SESSION['state']);
                    unset($_SESSION['access_token']);
                    return false;
                }
            }
        }

        return false;
    }

    public function light_check_connection() {
        //if(session_id()==""){
        session_name($_SERVER["REMOTE_ADDR"] . "-preuvideo");
        session_start();
        //}
        if (isset($_SESSION['state']) && isset($_SESSION['access_token'])) {
            return true;
        } else {
            return false;
        }
    }

}
